package com.atguigu.ssyx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.atguigu.ssyx.enums.user.User;
import com.atguigu.ssyx.enums.user.UserDelivery;
import com.atguigu.ssyx.user.mapper.UserMapper;
import com.atguigu.ssyx.user.service.IUserDeliveryService;
import com.atguigu.ssyx.user.service.IUserService;
import com.atguigu.ssyx.vo.user.LeaderAddressVo;
import com.atguigu.ssyx.vo.user.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-21
 */

@RequiredArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final UserMapper userMapper;

    private final IUserDeliveryService userDeliveryService;


    @Override
    public LeaderAddressVo getLeaderAddressByUserId(Long id) {
        //5.根据userid查询提货点和团长信息
        //user表里的id是与 user_delivery 表里的 user_id 联合查询 leader_id
        //然后根据leader_id到 leader 表里查询 leader_name

        return userMapper.getLeaderAddressByUserId(id);
    }

    @Override
    public UserLoginVo getUserLoginVo(Long id) {
        //1.根据用户id查询用户信息
        User user = userMapper.selectById(id);
        //2.封装UserLoginVo对象
        UserLoginVo userLoginVo = BeanUtil.copyProperties(user, UserLoginVo.class);
        //单独封装userId
        userLoginVo.setUserId(id);

        //查询用户团长id
        LambdaQueryWrapper<UserDelivery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDelivery::getUserId, id)
                .eq(UserDelivery::getIsDefault, 1);

        UserDelivery userDelivery = userDeliveryService.getOne(wrapper);
        if (userDelivery != null) {
            //抽取出团长id
            Long leaderId = userDelivery.getLeaderId();
            //封装团长id
            userLoginVo.setLeaderId(leaderId);
            //封装仓库id
            userLoginVo.setWareId(userDelivery.getWareId());
        } else {
            userLoginVo.setWareId(1L);
            userLoginVo.setLeaderId(1L);
        }


        return userLoginVo;
    }
}
